package com.qding.park.manager.app.queue.handle;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.app.queue.config.QueueTaskHandleConfig;
import com.qding.park.manager.cache.UserCacheInter;
import com.qding.park.manager.pojo.TbCar;
import com.qding.park.manager.pojo.queue.QueueMsg;
import com.qding.park.manager.pojo.sms.ExpireNotice;
import com.qding.park.manager.service.queue.mapper.TbCarMapper;
import com.qding.park.manager.sms.SmsInterfaceServiec;

@Component("expireNoticeHandle")
public class ExpireNoticeHandle extends AbstractAllHandle {

	private static final Log _log = LogFactory.getLog(ExpireNoticeHandle.class);
	@Autowired
	private TbCarMapper tbCarMapper;

	@Autowired
	private SmsInterfaceServiec smsInterfaceServiec;

	@Autowired
	private UserCacheInter userCacheInter;

	@Override
	void actualHandle(QueueMsg msg) throws BaseException {

		int expire_notice_days = QueueTaskHandleConfig.getExpire_notice_days();
		
		// 计算过期时间
		Date expireDate = DateUtils.addDay(new Date(), expire_notice_days);

//		List<TbCar> cars = tbCarMapper.queryExpireCar(expireDate);
		
		List<TbCar> cars = tbCarMapper.queryExpireNotice(DateUtils.dateFormat(expireDate));

		for (TbCar car : cars) {
			try {
				int userId = car.getUserID();
				String mobile = userCacheInter.userId2tel(String.valueOf(userId));
				if (!StringUtils.isBlank(mobile)) {
					ExpireNotice notice = new ExpireNotice();
					notice.setMobile(mobile);
					notice.setCarNum(car.getCarNum());
					notice.setExpireNotice(car.getCardExpiration());
					smsInterfaceServiec.expire_notice(notice);
				} else {
					_log.warn(car.getCarNum() + " 查询到手机号为空不发短信了！");
				}
			} catch (Exception e) {
				_log.error(e, e);
				exceptionHandle.handle(e);
			}
		}

	}

	@Override
	boolean failDiscardRollBacK(QueueMsg msg) {
		// TODO Auto-generated method stub
		return false;
	}

}
